#!/bin/bash

echo -e "阿新的云存储服务器....\n"
nginx -v
git --version
mvn --version
# This loads nvm
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm --version
node -v
echo -e "\n"
echo "root:$ROOT_PASSWD" | chpasswd
service ssh restart
service cron restart
service nginx restart
echo -e "\n"
echo -e "系统变量：$JPOM_VERSION,$JPOM_SERVER,$JPOM_AGENT,$GITEE_AGENT,$GITEE_AUTH,$GITEE_NAME,$GITEE_REMARK,$GITEE_COUNT...\n"

function get_json_value() {
    local json=$1
    local key=$2

    if [[ -z "$3" ]]; then
        local num=1
    else
        local num=$3
    fi
    local value=$(echo "${json}" | awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'${key}'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p)
    echo ${value}
}

if $GITEE_AGENT; then
    echo "启动Gitee插件端..."
    if [ -z "$(cat /root/gitee-agent/uuid)" ]; then
        echo "Gitee不存在"
        echo "-------downloading agent-------"
        curl -o /root/gitee-agent/agent.jar https://gitee-agent-shell.gz.bcebos.com/agent/agent.jar
        echo "-------agent download success-------"
        local_ip=''
        function get_local_ip() {
            ip_list=$(hostname -I || hostname -i)
            if [ "$ip_list" != "" ]; then
                local_ip=$(echo $ip_list | awk -F" " '{print $1}')
            fi
        }
        public_ip=''
        function get_public_ip() {
            set +e
            http_code=$(curl -w %{http_code} -s -m 3 ifconfig.co -o ./public_ip)
            if [ ${http_code} -eq 200 ]; then
                public_ip=$(cat ./public_ip)
            fi
            rm -f ./public_ip
            set -e
        }
        get_local_ip
        get_public_ip
        result=$(curl -X POST "https://server-agent.gitee.com/gitee_sa_server/sa/rest/v2/agents?${GITEE_AUTH}" -H 'Content-Type: application/json' -d "{\"agentName\": \"$GITEE_NAME\", \"intranetIp\": \"${local_ip}\", \"publicIp\": \"${public_ip}\", \"count\": \"$GITEE_COUNT\", \"remark\": \"$GITEE_REMARK\", \"maxProxyAgent\": \"\"}")
        echo 'result='${result}
        u_token=$(get_json_value "${result}" data)
        echo "UUID_TOKEN:" $u_token
        echo $u_token >/root/gitee-agent/uuid
    else
        echo "Gitee存在"
    fi
    cd /root/gitee-agent || exit
    if test -f run.log; then
        rm run.log
    fi
    echo "Gitee启动参数：$(cat /root/gitee-agent/uuid)"
    nohup java -jar -Dfile.encoding=UTF-8 agent.jar -s https://server-agent.gitee.com/gitee_sa_server -t $(cat /root/gitee-agent/uuid) >>run.log 2>&1 &
    sleep 10
    cat run.log
fi

if $JPOM_SERVER; then
    echo "启动Jpom服务端..."
    if [ -d "/root/jpom-server/bin" ]; then
        echo "Server目录存在"
    else
        echo "Server目录不存在"
        curl -o /root/server-release.tar.gz https://download.jpom.top/release/$JPOM_VERSION/server-$JPOM_VERSION-release.tar.gz
        cd /root && mkdir -p jpom-server
        tar -xzvf server-release.tar.gz -C ./jpom-server && rm -rf *-release.tar.gz
    fi
    cd /root/jpom-server || exit
    chmod 755 ./bin/*.sh
    ls -l ./bin
    nohup ./bin/Server.sh start 2>&1 &
    sleep 5
fi

if $JPOM_AGENT; then
    echo "启动Jpom插件端..."
    if [ -d "/root/jpom-agent/bin" ]; then
        echo "Agent目录存在"
    else
        echo "Agent目录不存在"
        curl -o /root/agent-release.tar.gz https://download.jpom.top/release/$JPOM_VERSION/agent-$JPOM_VERSION-release.tar.gz
        cd /root && mkdir -p jpom-agent
        tar -xzvf agent-release.tar.gz -C ./jpom-agent && rm -rf *-release.tar.gz
    fi
    cd /root/jpom-agent || exit
    chmod 755 ./bin/*.sh
    ls -l ./bin
    nohup ./bin/Agent.sh start 2>&1 &
    sleep 5
fi

while true; do
    sleep 5
done
